package cc.rengu.igas.cops.core.service.trans.intelliroute;

import cc.rengu.igas.cops.common.constant.CopsProductVersion;
import cc.rengu.igas.cops.core.model.ProcAddition;
import cc.rengu.igas.route.facade.request.IntelligenceResultQueryRequest;
import cc.rengu.igas.route.facade.response.IntelligenceResultQueryResponse;
import cc.rengu.igas.route.facade.result.Result;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 代收付鉴权结果查询
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/05/02
 */
public class IntelligenceResultQueryService extends BizBaseService {
    private static final String DUBBO_BEAN = "smartRouteService";
    private static final String DUBBO_METHOD = "resultQuery";

    @Override
    protected Object beforeBizProcess() throws Exception {
        IntelligenceResultQueryRequest request = new IntelligenceResultQueryRequest();
        ConvertUtil.convertOutput(request);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        Class c = Class.forName("cc.rengu.igas.cops.core.model." + CopsProductVersion.VERSION + ".ProcAdditionInfoImpl");
        ProcAddition procAddition = (ProcAddition) c.newInstance();
        request.setAdditionInfo(procAddition.procAdditionInfo(request.getHeader().getInstId(), request.getAdditionInfo()));
        if (!StringUtil.isEmptyOrNull(request.getAdditionInfo())) {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK4, request.getAdditionInfo());
        }
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        BizResponse bizResponse = new BizResponse();
        IntelligenceResultQueryRequest resultQueryRequest = (IntelligenceResultQueryRequest) request;
        //结果查询不登记非金融流水表
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (StringUtil.isEmptyOrNull(resultQueryRequest.getHeader().getTraceNo())) {
            resultQueryRequest.getHeader().setTraceNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
            xmlTreeUtil.copyStringValue(TreeNodeConstant.SYS_SEQ_NUM, TreeNodeConstant.HEADER_TRACE_NO);
        }
        //调用route模块dubbo服务-结果查询
        DubboService dubboService = new DubboServiceImpl();
        Result<IntelligenceResultQueryResponse> result = (Result<IntelligenceResultQueryResponse>) dubboService.callDubboService(DUBBO_BEAN, DUBBO_METHOD, resultQueryRequest);
        if (null == result || null == result.getResult()) {
            bizResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
        } else {
            ConvertUtil.convertInput(result.getResult());
            String dstChannelId = result.getResult().getDstChannelId();
            rglog.info("dstChannelId={}", dstChannelId);
            if (!StringUtil.isEmptyOrNull(dstChannelId)) {
                bizResponse.setRspSysId(dstChannelId);
            } else {
                bizResponse.setRspSysId("IGAS");
            }
            bizResponse.setRespDesc(result.getResult().getRespDesc());
            bizResponse.setRespCode(result.getResult().getRespCode());
            bizResponse.setResult(result.getResult());
        }
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
